/*
 * @(#)SqlMapClientProvider.java   08/12/24
 *
 *
 */



package net.learntechnology.examples.guice;

//~--- non-JDK imports --------------------------------------------------------

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;
import java.io.Reader;

/**
 *
 * @author nmaves
 */
public class SqlMapClientProvider implements Provider<SqlMapClient> {
	private static SqlMapClient sqlMapClient;
	@Inject @Named("ibatis.configXml")
	private String              configXml;

	public SqlMapClient get() {
		if (sqlMapClient != null) {
			return sqlMapClient;
		}

		try {
			Reader reader = Resources.getResourceAsReader(configXml);

			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();

			throw new RuntimeException("SqlMapClientP unable to create instance." + e);
		}

		return sqlMapClient;
	}
}
